# -*- coding: utf-8 -*-
"""
-------------------------------------------------
Project Name: luffcity study project
File Name: test1.py
Author: kij
Create Date: 2021/3/13
-------------------------------------------------
"""
import hashlib

def md5(origin):
    m = hashlib.md5()
    m.update(origin.encode('utf-8'))
    return m.hexdigest()

key = "192006250b4c09247ec02edce69f6a2d"
info = {
	"appid": "wxd930ea5d5a258f4f",
	"mch_id": "10000100",
	"device_info": "1000",
	"body": "test",
	"nonce_str": "ibuaiVcKdpRxkhJA"
}
data = sorted(info.items(), key=lambda x: x[0])
temp_list = []
for item in data:
	# *item 相当于把item里面的元组 ('appid', 'wxd930ea5d5a258f4f')打散一个一个赋值给{}和{}
	ele = "{}={}".format(*item)
	temp_list.append(ele)
string = "&".join(temp_list)
string_sign_temp = string+"&key={}".format(key)
sign =md5(string_sign_temp).upper()
info['sign'] = sign

result_list = []
for k,v in info.items():
	ele = "<{0}>{1}</{0}>".format(k,v)
	result_list.append(ele)
result = "<xml>{}</xml>".format("".join(result_list))
print(result)

# 方式二
# 列表生成式 把数据拼起来
string = "&".join(["{}={}".format(*item) for item in sorted(info.items(), key=lambda x: x[0])])
string_sign_temp = string + "&key={}".format(key)
sign = md5(string_sign_temp).upper()
info['sign'] = sign
result = "<xml>{}</xml>".format("".join(["<{0}>{1}</{0}>".format(k, v) for k, v in info.items()]))
